package com.hdoit.ares.logical.official.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.official.dao.IArticalClassifyDao;
import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.service.IArticalClassifyService;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifySearch;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifyTree;
import com.hdoit.ares.selfcommon.enums.official.ArticalClassifyType;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.selfcommon.enums.superbase.MenuType;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月08日 17:12:29
 * @comment
 */
@Component
public class ArticalClassifyServiceImpl extends SuperService<ArticalClassify> implements IArticalClassifyService{
	private IArticalClassifyDao articalClassifyDao;
	
	public IArticalClassifyDao getArticalClassifyDao() {
		return articalClassifyDao;
	}
	@Resource
	public void setArticalClassifyDao(IArticalClassifyDao articalClassifyDao) {
		this.articalClassifyDao = articalClassifyDao;
	}


	@Override
	public List<ArticalClassifyTree> queryMenuTreeList(String... useType) {
		List<ArticalClassifyTree> menuTreeList=queryListTop(useType);
		if(menuTreeList==null||menuTreeList.size()<=0){
			return null;
		}
		
		List<ArticalClassifyTree> allMenus=queryListMenu(useType);
		List<ArticalClassifyTree> hrefs=queryListHref(useType);
		List<ArticalClassifyTree> appMenus=queryListAppMenu(useType);
		allMenus.addAll(hrefs);
		allMenus.addAll(appMenus);
		if(allMenus!=null&&allMenus.size()>0){
			selMenu(allMenus,menuTreeList);
		}
		
		return menuTreeList;
	}
	
	
	private void selMenu(List<ArticalClassifyTree> allMenus, List<ArticalClassifyTree> menuTreeList) {
		if(menuTreeList==null||menuTreeList.size()==0){return;}
		int depth=1;
		for (ArticalClassifyTree  menuTree : menuTreeList) {
			if(StringUtils.isEmpty(menuTree.getDepth())){
				menuTree.setDepth(""+depth);
				depth++;
			}
			List<ArticalClassifyTree> childMenuTreeList=queryMenuByParentId(allMenus,menuTree);
			menuTree.setChildren(childMenuTreeList);
			
			selMenu(allMenus,childMenuTreeList);
		}
		
	}
	
	
	private List<ArticalClassifyTree> queryMenuByParentId(List<ArticalClassifyTree> allMenus,
			ArticalClassifyTree parent) {
		List<ArticalClassifyTree> childList=new ArrayList<ArticalClassifyTree>();
		int depth=1;
		for (ArticalClassifyTree childMenuTree : allMenus) {
			if(parent.getId().equals(childMenuTree.getParentId())){
				childMenuTree.setDepth(parent.getDepth()+"."+depth);
				childList.add(childMenuTree);
				depth++;
			}
		}
		return childList;
	}
	
	@Override
	public List<ArticalClassifyTree> queryListAppMenu(String[] useType) {
		ArticalClassifySearch search=new ArticalClassifySearch();
		search.setType(ArticalClassifyType.appMenu);
		search.setUseType(useType);
		
		return articalClassifyDao.queryListMenuTree(search);
	}
	
	@Override
	public List<ArticalClassifyTree> queryListHref(String[] useType) {
		ArticalClassifySearch search=new ArticalClassifySearch();
		search.setType(ArticalClassifyType.href);
		search.setUseType(useType);
		
		return articalClassifyDao.queryListMenuTree(search);
	}
	
	@Override
	public List<ArticalClassifyTree> queryListMenu(String[] useType) {
		ArticalClassifySearch search=new ArticalClassifySearch();
		search.setType(ArticalClassifyType.menu);
		search.setUseType(useType);
		
		return articalClassifyDao.queryListMenuTree(search);
	}
	
	@Override
	public List<ArticalClassifyTree> queryListTop(String[] useType) {
		ArticalClassifySearch search=new ArticalClassifySearch();
		search.setType(ArticalClassifyType.top);
		search.setUseType(useType);
		
		return articalClassifyDao.queryListMenuTree(search);
	}
	
	@Override
	public List<ArticalClassify> queryListTopMenu(String... useType) {
		ArticalClassifySearch search=new ArticalClassifySearch();
		search.setUseType(useType);
		search.setTypes(Arrays.asList(MenuType.top,MenuType.menu));
		
		return articalClassifyDao.queryList(search);
	}
	
	/*根据类型和父级Id查询所有列表*/
	@Override
	public List<ArticalClassify> queryOnlineSerList(String type, String parentId) {
		
		return articalClassifyDao.queryOnlineSerList(type, parentId);
	}
	
	
	@Override
	public List<ArticalClassify> queryList(ArticalClassifySearch search) {
		return articalClassifyDao.queryList(search);
	}
	@Override
	public List<ArticalClassify> queryList(String functionURL) {
		ArticalClassifySearch search = new ArticalClassifySearch();
		search.setFunctionURL(functionURL);
		ArticalClassify articalClassify = articalClassifyDao.queryModel(search);
		
		return articalClassifyDao.queryOnlineSerList(ArticalClassifyType.menu, articalClassify.getId());
	}
	@Override
	public ArticalClassify queryModel(ArticalClassifySearch articalClassifySearch) {
		return articalClassifyDao.queryModel(articalClassifySearch);
	}

}